import networkx as nx
import matplotlib.pyplot as plt
from sklearn.cluster import KMeans

# 生成模拟社交网络图
G = nx.erdos_renyi_graph(100, 0.05)
adj_matrix = nx.adjacency_matrix(G).toarray()

# 执行聚类
kmeans = KMeans(n_clusters=3, random_state=42)
clusters = kmeans.fit_predict(adj_matrix)

# 可视化网络聚类结果
pos = nx.spring_layout(G)
plt.figure(figsize=(10,8))
nx.draw_networkx_nodes(G, pos, node_color=clusters, cmap='viridis', node_size=50)
nx.draw_networkx_edges(G, pos, alpha=0.2)
plt.title('Social Network Community Detection')
plt.show()